package n13;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static int romanToInt(String s) {
        Map<String, Integer> romanIntMap = new HashMap<String, Integer>() {{
            put("I", 1);
            put("V", 5);
            put("X", 10);
            put("L", 50);
            put("C", 100);
            put("D", 500);
            put("M", 1000);
        }};
        Map<String, Integer> romanIntMinusMap = new HashMap<String, Integer>() {{
            put("IV", 4);
            put("IX", 9);

            put("XL", 40);
            put("XC", 90);

            put("CD", 400);
            put("CM", 900);
        }};

        int result = 0;
        String[] split = s.split("");
        for (int i = 0; i < split.length; i++) {
            String c = split[i];
            Integer num = romanIntMap.get(c);
            Integer nextNum = null;
            if (i + 1 < s.length()) {
                // 下个字符
                String n = split[i + 1];
                nextNum = romanIntMinusMap.get(c + n);
                if (nextNum != null) {
                    i++;// 跳过
                    result += nextNum;
                    continue;
                }
            }
            result += num;
        }

        return result;
    }

    public static void main(String[] args) {
        String[] cases = {
                // "III",
                "MCMXCIV"};
        for (String s : cases) {
            System.out.println(romanToInt(s));
        }
    }
}
